package com.aoxin.util.log;

import java.io.IOException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.aoxin.model.system.SlfOperateLog;
import com.aoxin.service.inter.system.OperateLogService;
import com.aoxin.util.AjaxRes;
import com.aoxin.util.ErrorUtil;
import com.aoxin.util.ThreadLocalUtil;
import com.aoxin.util.enumerate.OperateType;
import com.aoxin.util.enumerate.UserType;

/**
 * 日志工具类
 * 
 * @author Melody
 * @date 2018年3月26日 下午4:29:13
 */
@Aspect
@Component
public class LogAspect {

	private static Log log = LogFactory.getLog(LogAspect.class);

	@Autowired
	private OperateLogService operateLogService;

	@Around("@annotation(com.aoxin.util.log.LogInfo)")
	public Object process(ProceedingJoinPoint proceedingJoinPoint) {
		Object object = null;
		MethodSignature ms = (MethodSignature) proceedingJoinPoint.getSignature();
		// 获取方法
		Method method = ms.getMethod();
		// 获取类名
		String className = proceedingJoinPoint.getTarget().getClass().getName();
		// 获取注解
		LogInfo logInfo = (LogInfo) method.getAnnotation(LogInfo.class);
		// 获取操作类型
		OperateType operateType = logInfo.operateType();
		// 获取用户类型
		UserType[] userTypes = logInfo.userType();
		// 获取操作名
		String operateName = operateType.getValue();
		// 获取备注
		String content = logInfo.content();

		SlfOperateLog slfOperateLog = new SlfOperateLog();
		slfOperateLog.setContent("[" + content + "] [操作位置]:[" + className + ":" + method.getName() + "]");
		slfOperateLog.setType(operateType.getId());
		slfOperateLog.setOperateDate(new Date());
		for (int i = 0; i < userTypes.length; i++) {
			try {
				slfOperateLog.setOperaterId(ThreadLocalUtil.getUser().getUserId());
				slfOperateLog.setOperaterName(ThreadLocalUtil.getUser().getName());
			} catch (Exception e) {
				try {
					slfOperateLog.setOperaterId(ThreadLocalUtil.getEmployee().getEmployeeId());
					slfOperateLog.setOperaterName(ThreadLocalUtil.getEmployee().getName());
				} catch (Exception e2) {
					/*try {
						HttpServletResponse response = ThreadLocalUtil.getResponse();
						response.setContentType("text/json; charset=UTF-8");
						response.setCharacterEncoding("utf-8");
						AjaxRes ajaxRes = new AjaxRes();
						ajaxRes.setLogoutMsg(ErrorUtil.LOGIN_EXPIRE);
						response.getWriter().write(ajaxRes.toJSONString());
						response.getWriter().close();
					} catch (IOException e1) {
						e1.printStackTrace();
					}*/
				}
			}
		}
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(slfOperateLog.getOperateDate());
		try {
			object = proceedingJoinPoint.proceed();
			slfOperateLog.setContent(slfOperateLog.getContent() + " [操作成功]");
		} catch (Throwable e) {
			slfOperateLog.setContent(slfOperateLog.getContent() + " [操作失败]:[" + e.getMessage() + "]");
		}
		// 打印日志
		log.info("[" + slfOperateLog.getOperaterName() + "]在[" + dateString + "][" + operateName + "]" + slfOperateLog.getContent());
		this.operateLogService.insertOperateLog(slfOperateLog);
		return object;
	}
}
